package vn.vttu.baocaocuoiki;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;

import javax.security.auth.login.LoginException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class Register extends Activity {
	private Spinner spnPermission;
	private EditText edtUser_Register;
	private EditText edtPass_Register;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		addListenerOnSpinnerItemSelection();
		ThamChieu();
	}
	private void ThamChieu()
	{
		edtUser_Register = (EditText)findViewById(R.id.edtUser_Register);
		edtPass_Register = (EditText)findViewById(R.id.edtPass_Register);
		spnPermission = (Spinner)findViewById(R.id.spnPermission);
	}
	private void addListenerOnSpinnerItemSelection() {
		spnPermission = (Spinner) findViewById(R.id.spnPermission);
		spnPermission.setOnItemSelectedListener(new CustomOnItemSelectedListener());
	}
	
	public void OK(View view) {
		if(ghiFile("DS_ThanhVien.txt",edtUser_Register.getText().toString() + "#" + edtPass_Register.getText().toString() + "#" + spnPermission.getSelectedItem().toString()));
		{
			Toast.makeText(getApplicationContext(), "Đăng kí thành công", Toast.LENGTH_SHORT).show();
			edtUser_Register.setText("");
			edtPass_Register.setText("");
		}	
	}	
	public void Login(View view) {
		Intent login = new Intent(getApplicationContext(),MainActivity.class);
		startActivity(login);
	}
	
    //Phương thức ghi nội dung ra tệp tin.
  	public boolean ghiFile (String tenfile, String noiDungFile){
  		boolean bool = true;
  		java.io.FileOutputStream fileOutputStream = null; 
  		java.io.OutputStreamWriter outputStreamWriter = null; 
  		java.io.BufferedWriter bufferedWriter = null;
  		try {
  			// B1 : Khai báo đối tượng File lưu trữ.
  			fileOutputStream = openFileOutput(tenfile, Context.MODE_APPEND|Context.MODE_PRIVATE);
  			// B2 : Khai báo đối tượng ghi ra File lưu trữ.
  			outputStreamWriter = new OutputStreamWriter(fileOutputStream);
  			bufferedWriter = new BufferedWriter(outputStreamWriter);
  		    // B3 : Ghi nội dung ra File lưu trữ.
  			bufferedWriter.write(noiDungFile + "\n");
  			bufferedWriter.close();
  		} catch (Exception e) {
  			bool = false;
  			Toast.makeText(getApplicationContext(), "Có lỗi trong quá trình ghi File."+e.toString() , Toast.LENGTH_LONG).show();
  		}
  		return bool;
  				
  	}	
}
